﻿using NewsDiscoveryEngine;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Web;
using NewsMine.Storage;
using NewsMine.Utilities;
using NewsMine.DomainObjects;

namespace NewsMine.JobEngine.Tests
{
    
    
    /// <summary>
    ///This is a test class for JobsManagerTest and is intended
    ///to contain all JobsManagerTest Unit Tests
    ///</summary>
    [TestClass()]
    public class JobsManagerTest
    {
        private TestContext testContextInstance;

        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }

        [TestMethod()]
        public void TestIndexing()
        {
            IndexHtmlFeedItemJob job = new IndexHtmlFeedItemJob();

            while (job != null)
            {
                job = (IndexHtmlFeedItemJob)Defaults.UIQueue.Dequeue();

                if (job != null && job is IndexHtmlFeedItemJob)
                {
                    job.ExecuteJob();
                }
            }
            
        }

        [TestMethod()]
        public void TestIndexSeachTest()
        {
            string htmlEncoded = "&#3116;&#3137;&#3093;&#3149; &#3118;&#3134;&#3120;&#3149;&#3093;&#3149;&#3128;&#3149;";
            var result = HttpUtility.HtmlDecode(htmlEncoded);

            return;

            FeedIndexSearcher indexer = FeedIndexSearcher.Instance;

            var items = indexer.GetFeedItemsByDomain("telugu.oneindia.in");

        }

        [TestMethod()]
        public void TestReIndexing()
        {
            FeedIndexer indexer = FeedIndexer.Instance;

            indexer.ReindexFromSqliteStore();

            indexer.OptimizeIndex();
            indexer.OptimizeIndex();
            indexer.OptimizeIndex();
            indexer.OptimizeIndex();


        }

        [TestMethod()]
        public void TestSelfReIndexing()
        {
            FeedIndexer indexer = FeedIndexer.Instance;

            indexer.DoSelfReIndex();
        }

        [TestMethod()]
        public void TestOptimizeIndex()
        {
            FeedIndexer indexer = FeedIndexer.Instance;

            indexer.OptimizeIndex();




        }

        [TestMethod]
        public void ScheduleJobs()
        {
            ProcessWebUrl2FeedWorkFlow wf = new ProcessWebUrl2FeedWorkFlow();
            wf.Initialize();

        }


        [TestMethod()]
        public void StartJobEngineTest()
        {
            //test with hit url job 

            //HtmlDownloadJob job = new HtmlDownloadJob();
            //job.Url = "http://ibnlive.in.com/news/mumbai-police-to-give-up-aseem-trivedis-custody/290209-3-237.html";
            //job.WorkFlow = new HintWebUrl2WebSiteInfoCollector();

            //Defaults.JobQueue.Enqueue(job);

            //this is just schedule the crawling jobs.

            if (true)  //just ignoring this part for a movement
            {
                NewsMine.DomainObjects.WebSiteMenu menuItem = new DomainObjects.WebSiteMenu { Url = "http://www.andhrabhoomi.net/news/Sports", Domain = "www.andhrabhoomi.net", Category = "Sports" };

                HtmlDownloadJob downLoadJob = new HtmlDownloadJob();
                downLoadJob.Url = menuItem.Url;
                downLoadJob.WorkFlow = new ProcessWebUrl2FeedWorkFlow();

                Defaults.JobQueue.Enqueue(downLoadJob);

                Defaults.CrawlingTrackingStore.Replace(menuItem.Url, menuItem);
                Defaults.CrawlingTrackingStore.Close();
            }
            JobsManager jobsManager = new JobsManager();

            jobsManager.ProcessJobsByJobEngine();
        }

        [TestMethod()]
        public void UrlAsKeyInIndexTest()
        {
            string url = "http://ibnlive.in.com/news/google-india-blocks-access-to-antiislam-film/292092-3.html";

            var objects = Defaults.UniqueHtmlFeedLinkStore.Get(url);

            return;



            KeyValueStoreManagerOnFileSystem kvStoreMgr = new KeyValueStoreManagerOnFileSystem();
            
            var kvStore = kvStoreMgr.GetKeyValueStoreByName("temp");
            if (kvStore == null)
            {
                kvStoreMgr.CreateKeyValueStore("temp");
                kvStore = kvStoreMgr.GetKeyValueStoreByName("temp");
            }
            
            string key = "http://telugu.oneindia.in/talk-of-the-day/~/akdfjda/~kljl";
            kvStore.Set(key, new object());

            object obj = kvStore.Get(key);

            kvStore.Delete(key);

          
        }


        [TestMethod()]
        public void SimulateWorkflowProcess()
        {
            try
            {
                if (Defaults.JobQueue.GetCount() == 0)
                {
                    TempLogger.Log("Feed processing started...");
                    new ProcessWebUrl2FeedWorkFlow().Initialize();
                    TempLogger.Log("Job scheduling over in wf.Initialize method.");
                }
                else
                {
                    TempLogger.Log("Job scheduling skipped.");
                }
                TempLogger.Log("Process jobs by job engine... started");
                JobsManager jobsManager = new JobsManager();

                jobsManager.ProcessJobsByJobEngine();
                TempLogger.Log("Process jobs by job engine... end...");
            }
            catch (Exception ex)
            {
                TempLogger.Log("Error while processing Feed retrieval from web pages", ex);
            }
            finally
            {
                
            }
        }

         [TestMethod()]
        public void CaseSensitiveUrlTest()
        {
            HtmlContentFromUrl content = new HtmlContentFromUrl();

            content.HtmlContent = System.IO.File.ReadAllText("c:\\testhtml.txt");
            content.Url = "http://www.sumanasa.com/";
            var results  = HtmlString2LinksJob.GetLinksFromHtmlContent(content);
        
        }


       

    }

    



}
